<?php
/**
 *title
 * @author: lican<364548233@qq.com>
 *Date: 2020/11/3
 *Time: 14:15
 */
namespace App\Http\Helpers;

use App\Http\Services\MsgService;
use Toplan\Sms\SmsManager;
use App\Models\SmsCodeRecord;

class Sms{

    const EXPIRE = 10;

    public static function sendCode($mobile){
        $res = self::check($mobile);
        if(!$res){
            return MsgService::getMsg(100050);
        }
//        $template = 'SMS_205392982'; //aliyun
        $template = config('easysms.gateways.Qcloud.tpl_id'); //腾讯云
        $easySms = app('easysms');
        $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数，左侧补0
        $message = '发送成功';
        try{
            $res = $easySms->send($mobile, [
                'template' => $template,
                'data' => [$code],
            ]);
        }catch(\Exception $e){
            $res = $e->getMessage();
        }
        self::recordSmsCode($mobile, $code, $template, json_encode($res), $message);
        $expiredAt = now()->addMinutes(10);
        // 缓存验证码 10 分钟过期。
        app('redis')->setex($mobile, 600, $code);

        return MsgService::getMsg(0, [
            'mobile' => $mobile,
            'expired_at' => $expiredAt->toDateTimeString(),
        ]);
    }

    /**
     *记录短信验证码
     * @author: lican<364548233@qq.com>
     */
    public static function recordSmsCode($mobile, $code, $tpl, $content = '', $message = ''){
        $smsCodeRecord = new SmsCodeRecord();
        $smsCodeRecord->mobile = $mobile;
        $smsCodeRecord->tpl_id = $tpl;
        $smsCodeRecord->code = $code;
        $smsCodeRecord->content = $content;
        $smsCodeRecord->remark = $message;
        return $smsCodeRecord->save();
    }

    /**
     *验证手机验证码
     * @author: lican<364548233@qq.com>
     */
    public static function checkCode($mobile, $code){
        if(empty($code) || strlen($code) != 4){
            return MsgService::getMsg(100052);
        }
        $verify = app('redis')->get($mobile);

        if(empty($verify)){
            return MsgService::getMsg(100051);
        }
        if($code !== $verify){
            return MsgService::getMsg(100052);
        }
        return MsgService::getMsg(0);
    }

    /**
     *验证手机号码
     * @author: lican<364548233@qq.com>
     */
    public static function check($phone)
    {
        return preg_match('/^\(\+(\d+)\)\s?(\d+)$|^1\d{10}$/', $phone);
    }

    //是否带有区号
    public static function checkAreaCode($phone)
    {
        $bool = preg_match('/^\(\+(\d+)\)\s?(\d+)$/', $phone, $result);
        return [$bool, $result];
    }

    //收拢对大陆手机的规则校验,国际号码不做校验
    public static function checkAndSend($phone)
    {
        list($bool, $result) = self::checkAreaCode($phone);
        // 没有带区号或者区号是86,都认为是大陆手机号
        if (!$bool || $result[1] == '86') {
            if (!preg_match('/1\d{10}/', $phone)) {
                return [
                    'code' => -1,
                    'message' => '手机号码格式错误',
                ];
            }
        }
    }
}
